package fr.um2.physique.risa.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * This class displays a tab for the gaussian convolution function. (view of
 * pattern mvc)
 * 
 * @author Mathilde Salthun-Lassalle
 */
public class GaussConvolveTab extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	// param�tres
	JButton b13;
	JFrame frame;
	GaussConvolveController controller = new GaussConvolveController();

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	/**
	 * Creates a TabbedPanel for gaussian convolution
	 * 
	 * @return picturePane
	 */
	protected JPanel Picture() {
		GridLayout experimentLayout = new GridLayout(0, 1);

		// Create Button
		b13 = new JButton("Gaussian Convolution");
		b13.addActionListener(this);

		// Create Panel
		JPanel picturePane = new JPanel();
		JLabel titre = new JLabel("Picture Display");
		picturePane.add(titre);
		picturePane.setLayout(experimentLayout);
		picturePane.add(b13);

		return picturePane;
	}

	/**
	 * The Action Listener To display fits pictures For saveAs button
	 */
	public void actionPerformed(ActionEvent e) {

		try {
			if (e.getSource() == b13) {

				String sigma_str = (String) controller.askForParameter("sigma");
				float sigma = Float.valueOf(sigma_str.trim()).floatValue();
				
				
				String radius_str = (String) controller
						.askForParameter("radius");
				int radius = new Integer(radius_str).intValue();
				
								
				controller.displayGaussianConvolution(radius, sigma);

			}
		}
		 catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(this,
					"cause : the parameter is not a number.", "Format Error",
					JOptionPane.WARNING_MESSAGE);
			
		} catch (NullPointerException npe) {
			JOptionPane.showMessageDialog(this, "cause : File not found.",
					"File  Error", JOptionPane.WARNING_MESSAGE);
			
		}catch (IllegalArgumentException nvp) {
			JOptionPane.showMessageDialog(this,
					"cause : the parameter is not a valid number : \n"+ nvp.getMessage(), "Format Error",
					JOptionPane.WARNING_MESSAGE);
			
		}

	}

	/**
	 * displays an input box
	 * 
	 * @param parameter
	 *            to display
	 * @return string its value
	 */
	public String displayInputForParameter(String parameter) {
		return (String) JOptionPane.showInputDialog(this, "Enter value for "
				+ parameter, "Input Box", JOptionPane.PLAIN_MESSAGE, null,
				null, "Add");
	}

}
